package com.ruoyi.animal.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.animal.domain.CommerceInfoAnimal;
import com.ruoyi.animal.service.ICommerceInfoAnimalService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.animal.mapper.AnimalInfoMapper;
import com.ruoyi.animal.domain.AnimalInfo;
import com.ruoyi.animal.service.IAnimalInfoService;

/**
 * 寄养动物信息Service业务层处理
 * 
 * @author zhezhi
 * @date 2022-10-02
 */
@Service
public class AnimalInfoServiceImpl implements IAnimalInfoService 
{
    @Autowired
    private AnimalInfoMapper animalInfoMapper;

    @Autowired
    private ICommerceInfoAnimalService animalService;

    /**
     * 查询寄养动物信息
     * 
     * @param animalId 寄养动物信息主键
     * @return 寄养动物信息
     */
    @Override
    public AnimalInfo selectAnimalInfoByAnimalId(Long animalId)
    {
        AnimalInfo animalInfo = animalInfoMapper.selectAnimalInfoByAnimalId(animalId);
        CommerceInfoAnimal commerceInfoAnimal = animalService.selectCommerceInfoAnimalByAnimalId(animalId);
        animalInfo.setCommerceId(commerceInfoAnimal.getInfoId());
        return animalInfo;
    }

    /**
     * 查询寄养动物信息列表
     * 
     * @param animalInfo 寄养动物信息
     * @return 寄养动物信息
     */
    @Override
    public List<AnimalInfo> selectAnimalInfoList(AnimalInfo animalInfo)
    {
        return animalInfoMapper.selectAnimalInfoList(animalInfo);
    }

    /**
     * 新增寄养动物信息
     * 
     * @param animalInfo 寄养动物信息
     * @return 结果
     */
    @Override
    public int insertAnimalInfo(AnimalInfo animalInfo)
    {
        animalInfo.setCreateTime(DateUtils.getNowDate());
        int i = animalInfoMapper.insertAnimalInfo(animalInfo);
        AnimalInfo animalInfo1 = animalInfoMapper.selectAnimalInfoByEnd();
        CommerceInfoAnimal infoAnimal = new CommerceInfoAnimal();
        infoAnimal.setAnimalId(animalInfo1.getAnimalId());
        infoAnimal.setInfoId(animalInfo.getCommerceId());
        int i1 = animalService.insertCommerceInfoAnimal(infoAnimal);
        return i;
    }

    /**
     * 修改寄养动物信息
     * 
     * @param animalInfo 寄养动物信息
     * @return 结果
     */
    @Override
    public int updateAnimalInfo(AnimalInfo animalInfo)
    {
        animalInfo.setUpdateTime(DateUtils.getNowDate());
        CommerceInfoAnimal commerceInfoAnimal = animalService.selectCommerceInfoAnimalByAnimalId(animalInfo.getAnimalId());
        if(commerceInfoAnimal.getInfoId() != animalInfo.getCommerceId()){
            CommerceInfoAnimal commerceInfoToAnimal = new CommerceInfoAnimal();
            commerceInfoToAnimal.setAnimalId(animalInfo.getAnimalId());
            commerceInfoToAnimal.setInfoId(animalInfo.getCommerceId());
            animalService.updateCommerceInfoAnimalByAnimal(commerceInfoToAnimal);
    }
        return animalInfoMapper.updateAnimalInfo(animalInfo);
    }

    /**
     * 批量删除寄养动物信息
     * 
     * @param animalIds 需要删除的寄养动物信息主键
     * @return 结果
     */
    @Override
    public int deleteAnimalInfoByAnimalIds(Long[] animalIds)
    {

        for(Long Id:animalIds){
            CommerceInfoAnimal commerceInfoAnimal = animalService.selectCommerceInfoAnimalByAnimalId(Id);
            int i = animalService.deleteCommerceInfoAnimalByInfoId(commerceInfoAnimal.getInfoId());
        }
//        int i1 = animalService.deleteCommerceInfoAnimalByInfoIds(CommerceIds);
        return animalInfoMapper.deleteAnimalInfoByAnimalIds(animalIds);
    }

    /**
     * 删除寄养动物信息信息
     * 
     * @param animalId 寄养动物信息主键
     * @return 结果
     */
    @Override
    public int deleteAnimalInfoByAnimalId(Long animalId)
    {
        CommerceInfoAnimal commerceInfoAnimal = animalService.selectCommerceInfoAnimalByAnimalId(animalId);
        int i = animalService.deleteCommerceInfoAnimalByInfoId(commerceInfoAnimal.getInfoId());
        return animalInfoMapper.deleteAnimalInfoByAnimalId(animalId);
    }
}
